//
//  UIImage + Extension.swift
//  微博
//
//  Created by 余仿 on 16/4/15.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit

extension UIImage{

    class func cutImage() -> UIImage{

        //需要被绘制的视图
        let window = UIApplication.sharedApplication().keyWindow

        //        1.开启图形上下文
        let size = UIScreen.mainScreen().bounds.size

        //        UIGraphicsBeginImageContext(size)
        /*
        参数说明
        <#T##size: CGSize##CGSize#>返回图片的尺寸
        <#T##opaque: Bool##Bool#>:   A Boolean flag indicating whether the bitmap is opaque. If you know the bitmap is fully opaque, specify true to ignore the alpha channel and optimize the bitmap’s storage. Specifying false means that the bitmap must include an alpha channel to handle any partially transparent pixels.
        不透明度?
        <#T##scale: CGFloat##CGFloat#>:The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.???
        */
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.mainScreen().scale)

        //        2.绘制
        window?.drawViewHierarchyInRect(UIScreen.mainScreen().bounds, afterScreenUpdates: true)

        //        3.从上下文获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //        4.关闭图形上下文
        UIGraphicsEndImageContext()
        
        //        5.返回
        return image
    }
}